package com.cos.hospital.ui.login;

import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cos.hospital.R;
import com.cos.hospital.code.Code;
import com.cos.hospital.code.helper.KTextWatcher;
import com.cos.hospital.init.BaseActivity;
import com.cos.hospital.model.UHttp;
import com.cos.hospital.model.bean.ErrorBean;
import com.cos.hospital.model.bean.ResultBean;
import com.cos.hospital.model.connector.HttpMsgListener;
import com.cos.hospital.tool.UDialog;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.widget.ProgressDialog;
import com.google.gson.JsonObject;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * @Description: 重置密码.2
 * @Author: Kosmos
 * @Date: 2018年9月10日 17:01
 * @Email: KosmoSakura@foxmail.com
 */
public class PasswordChangeActivity extends BaseActivity {
    @BindView(R.id.change_password)
    EditText edtOld;
    @BindView(R.id.change_confirm)
    EditText edtNew;
    @BindView(R.id.reset_done)
    TextView tDone;
    private String canDone;
    private String phone;
    private String password;
    private ULogin login;

    @Override
    protected int layout() {
        return R.layout.activity_password_change;
    }

    @Override
    protected void init() {
        phone = getIntent().getStringExtra(Code.SP.PHONE);
        setTitleLeftIcon();
        setTitleCenterTxt(getString(R.string.forget_password));
        checkDonebtn();
    }

    @Override
    protected void logic() {
        edtOld.addTextChangedListener(new KTextWatcher() {
            @Override
            public void afterTextChanged(Editable txtEnd) {
                if (txtEnd.length() > 16) {
                    showNotice(getString(R.string.password_format_error));
                }
                checkDonebtn();
            }
        });
        edtNew.addTextChangedListener(new KTextWatcher() {
            @Override
            public void afterTextChanged(Editable txtEnd) {
                if (txtEnd.length() > 16) {
                    showNotice(getString(R.string.password_format_error));
                }
                checkDonebtn();
            }
        });
    }

    private void checkDonebtn() {
        canDone = checkState();
        if (UTxt.isEmpty(canDone)) {
            tDone.setBackgroundResource(R.drawable.rect_green);
        } else {
            tDone.setBackgroundResource(R.drawable.rect_gray_light);
        }
    }

    private String checkState() {
        password = edtOld.getText().toString();
        if (UTxt.isEmpty(edtOld)) {
            return getString(R.string.password_new);
        }
        if (UTxt.isEmpty(edtNew)) {
            return getString(R.string.password_confirm_new);
        }
        if (edtOld.length() < 6 || edtOld.length() > 16) {
            return getString(R.string.password_about);
        }
        if (edtNew.length() < 6 || edtNew.length() > 16) {
            return getString(R.string.password_about);
        }
        if (!password.equals(edtNew.getText().toString())) {
            return getString(R.string.password_two);
        }
        return "";
    }


    @OnClick({R.id.title_back, R.id.reset_done})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.reset_done:
                if (UTxt.isEmpty(canDone)) {
                    resetPassword();
                } else {
                    showNotice(canDone);
                    edtNew.setText("");
                }
                break;
        }
    }

    private void resetPassword() {
        ProgressDialog.getInstance().startProgressDialog(this);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("loginPhone", phone);
        jsonObject.addProperty("newPassword", password);
        jsonObject.addProperty("newPassword2", password);
        UHttp.start(getServes().resetPassword(jsonObject), "修改密码", new HttpMsgListener<ResultBean>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onError(String describe) {
                showNotice(describe);
            }

            @Override
            public void onFinish() {
                ProgressDialog.getInstance().stopProgressDialog();
            }

            @Override
            public void onSuccess(ResultBean dto) {
                if (dto.isSuccess()) {
                    UDialog.getInstance(context, false, false)
                        .showNoticeWithTwobtn("密码修改成功!", (result, dia) -> {
                            dia.dismiss();
                            login = new ULogin(PasswordChangeActivity.this, getServes(), phone, password);
                            login.runLogin();
                        });
                } else {
                    ErrorBean errorBean = dto.getError();
                    if (errorBean != null) {
                        showNotice(UTxt.isNull(errorBean.getMessage(), Code.DefaultPrompt));
                    } else {
                        showNotice(Code.DefaultPrompt);
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (login != null) {
            login.onDestroy();
        }
        super.onDestroy();
    }
}
